package com.sky.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingServiceImpi implements ShoppingService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 添加购物车
    * */
    @Override
    public void shoppingadd(ShoppingCartDTO shoppingCartDTO) {
        //数据kaob
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车中是否有该数据
           List<ShoppingCart> list =  shoppingCartMapper.selectShopping(shoppingCart);
        if (list !=null && list.size()>0){
            //如果有就把数量加1
            shoppingCart  = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNUmberByid(shoppingCart);
        }else {
            //那就是不存在
            //获取当前插入的是菜品还是套餐
           Long dishId = shoppingCartDTO.getDishId();
            if (dishId !=null){
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            }else {
                //那就是套餐
                  Setmeal setmeal = setmealMapper.getBid(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.add(shoppingCart);
        }

    }
/*
* 查询购物车
* */
    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
       List<ShoppingCart> list = shoppingCartMapper.liseId(shoppingCart);
        return list;
    }
/*
* 清空购物车
* */
    @Override
    public void shoppingdclean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.cleanShopping(shoppingCart);

    }
/*
*
* 根据id删除商品
* */
    @Override
    public void subshopping(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
//        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先查询是否有还商品
        List<ShoppingCart> list = shoppingCartMapper.selectShopping(shoppingCart);
        if (list !=null && list.size()>0){
             shoppingCart = list.get(0);
             Integer numbr = shoppingCart.getNumber();
             if (numbr==1){
                 shoppingCartMapper.subshopping(shoppingCart);
             }else {
                 shoppingCart.setNumber(shoppingCart.getNumber()-1);
                 shoppingCartMapper.updateNUmberByid(shoppingCart);
             }

        }


    }
}
